import { Injectable } from '@angular/core';

@Injectable()
export class ProductService {
 private products:Product[] = [  // 调用定义的构造函数，创建了六个Product对象111
  new Product(1, "第一个商品", 1.99, 3.5, "这是第一个商品，Angular学习", ["电子产品", "学习视频", "硬件设备"]),
  new Product(2, "第二个商品", 2.99, 2.5, "这是第二个商品，Angular学习", ["电子产品", "硬件设备"]),
  new Product(3, "第三个商品", 3.5, 1.5, "这是第三个商品，Angular学习", ["电子产品", "学习视频"]),
  new Product(4, "第四个商品", 2.3, 3.5, "这是第四个商品，Angular学习", ["硬件设备"]),
  new Product(5, "第五个商品", 1.5, 4.5, "这是第五个商品，Angular学习", ["电子产品", "学习视频"]),
  new Product(6, "第六个商品", 6.3, 2.5, "这是第六个商品，Angular学习", ["硬件设备", "学习视频"])
]

private comments:Comment[] = [
  new Comment(1, 1, "2017-10-13 14:27:10", "张三", 3, "good"),
  new Comment(2, 2, "2017-6-13 4:27:26", "李四", 4, "well"),
  new Comment(3, 1, "2017-10-1 14:27:56", "网二", 3, "very good"),
  new Comment(4, 2, "2017-9-5 11:27:22", "赵六", 2, "bad"),
  new Comment(5, 3, "2017-10-13 14:27:22", "王五", 3, "good"),
  new Comment(6, 3, "2017-10-13 14:27:22", "周六", 2, "good"),
  new Comment(7, 2, "2017-10-13 14:27:22", "王琴", 4, "good"),
  new Comment(8, 2, "2017-10-13 14:27:22", "刘宇", 5, "很好"),
  new Comment(9, 4, "2017-10-13 14:27:22", "致力", 3, "还不错"),
  new Comment(10, 5, "2017-10-13 14:27:22", "王婷", 2, "一般"),
  new Comment(11, 6, "2017-10-13 14:27:22", "周柳", 3, "一般"),
]
  constructor() { }

  //获取所有商品列表
  getProducts(): Product[] {
    return this.products;
  }
  //根据商品id获取商品信息并返回
  getProduct (id:number): Product {
    return this.products.find((product) => product.id == id);
  }
  //根据商品id获取评论信息
  getCommentsForProductId(id: number) : Comment[] {
    return this.comments.filter((comment: Comment) => comment.productId == id);
  }


}
export class Product {     // 声明一个Product类，下有一个成员，即constructor构造函数，有六个公共参数
  constructor(
    public id: number,
    public title: string,
    public price: number,
    public rating: number,
    public desc: string,
    public categories: Array<string>
  ) {

  }
}

export class Comment {
  constructor(public id: number,
              public productId:number,
              public timestamp: string,
              public user: string,
              public rating: number,
              public content: string
  ){

  }
}
